package com.qqtech.qquan.order.dao;

import java.math.BigDecimal;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import com.qqtech.core.common.util.BeanUtil;
import com.qqtech.core.frame.dao.BaseDaoImpl;
import com.qqtech.qquan.order.model.OrderBase;
import com.qqtech.qquan.order.model.OrderBaseQuery;
import com.qqtech.qquan.order.model.OrderBaseVo;

@Repository
public class OrderBaseDaoImpl extends BaseDaoImpl<OrderBase> implements OrderBaseDao {
	public static final Logger log = LoggerFactory.getLogger(OrderBaseDaoImpl.class);

	public OrderBaseVo getByOrderNo(java.lang.String orderNo) {
		return sqlSessionTemplate.selectOne(getSqlName("selectByOrderNo"), orderNo);
	}

	@Override
	public Page<OrderBaseVo> queryPageListAdmin(OrderBaseQuery query, Pageable pageable) {
		List<OrderBaseVo> contentList = sqlSessionTemplate.selectList(getSqlName("selectAdmin"),
				getParams(query, pageable));
		return new PageImpl<OrderBaseVo>(contentList, pageable, this.queryCountAdmin(query));
	}

	@Override
	public List<OrderBaseVo> queryList4Excel(OrderBaseQuery query, Pageable pageable) {
		return sqlSessionTemplate.selectList(getSqlName("selectAdmin"), getParams(query, pageable));
	}

	@Override
	public int queryCountAdmin(OrderBaseQuery query) {
		return sqlSessionTemplate.selectOne(getSqlName("selectCountAdmin"), BeanUtil.toMap(query));
	}

	@Override
	public Page<OrderBaseVo> queryPageBySupplier(OrderBaseQuery query, Pageable pageable) {
		List<OrderBaseVo> contentList = sqlSessionTemplate.selectList(getSqlName("selectBySupplier"),
				getParams(query, pageable));
		return new PageImpl<OrderBaseVo>(contentList, pageable, this.queryCountBySupplier(query));
	}

	@Override
	public int queryCountBySupplier(OrderBaseQuery query) {
		return sqlSessionTemplate.selectOne(getSqlName("selectCountBySupplier"), BeanUtil.toMap(query));
	}

	@Override
	public Page<OrderBaseVo> queryPageBySendDeliveryUser(OrderBaseQuery query, Pageable pageable) {
		List<OrderBaseVo> contentList = sqlSessionTemplate.selectList(getSqlName("selectBySendDeliveryUser"),
				getParams(query, pageable));
		return new PageImpl<OrderBaseVo>(contentList, pageable, this.queryCountBySendDeliveryUser(query));
	}

	@Override
	public int queryCountBySendDeliveryUser(OrderBaseQuery query) {
		return sqlSessionTemplate.selectOne(getSqlName("selectCountBySendDeliveryUser"), BeanUtil.toMap(query));
	}

	@Override
	public int updateOrderStatusToCancle(OrderBaseQuery query) {
		return sqlSessionTemplate.update(getSqlName("updateOrderStatusToCancle"), BeanUtil.toMap(query));
	}

	@Override
	public int updateOrderStatusToSended(OrderBaseQuery query) {
		return sqlSessionTemplate.update(getSqlName("updateOrderStatusToSended"), BeanUtil.toMap(query));
	}

	@Override
	public int updateOrderStatusToSigned(OrderBaseQuery query) {
		return sqlSessionTemplate.update(getSqlName("updateOrderStatusToSigned"), BeanUtil.toMap(query));
	}

	@Override
	public int updateOrderStatusToPayed(OrderBaseQuery query) {
		return sqlSessionTemplate.update(getSqlName("updateOrderStatusToPayed"), BeanUtil.toMap(query));
	}

	@Override
	public int updateOrderReturnStatus(OrderBaseQuery query) {
		return sqlSessionTemplate.update(getSqlName("updateOrderReturnStatus"), BeanUtil.toMap(query));
	}

	@Override
	public BigDecimal queryTotalPriceByShop(OrderBaseQuery query) {
		return sqlSessionTemplate.selectOne(getSqlName("queryTotalPriceByShop"), BeanUtil.toMap(query));
	}

	@Override
	public int updateReturnPayStatus(OrderBaseQuery newOrderBaseOther) {
		return sqlSessionTemplate.update(getSqlName("updateReturnPayStatus"), BeanUtil.toMap(newOrderBaseOther));
	}

	@Override
	public int updateSellerTotalById(OrderBaseQuery query) {
		return sqlSessionTemplate.update(getSqlName("updateSellerTotalById"), BeanUtil.toMap(query));
	}

	@Override
	public BigDecimal getSumSellerTotal(OrderBaseQuery query) {
		BigDecimal sumSellerTotal=sqlSessionTemplate.selectOne(getSqlName("selectSumSellerTotal"), BeanUtil.toMap(query));
		return sumSellerTotal==null?BigDecimal.ZERO:sumSellerTotal;
	}
	
	@Override
	public BigDecimal getSumTotal4Admin(OrderBaseQuery query) {
		BigDecimal sumTotal=sqlSessionTemplate.selectOne(getSqlName("selectSumTotal4Admin"), BeanUtil.toMap(query));
		return sumTotal==null?BigDecimal.ZERO:sumTotal;
	}

	@Override
	public List<OrderBaseVo> queryOrderCountByUserIds(OrderBaseQuery query) {
		return sqlSessionTemplate.selectList(getSqlName("selectOrderCountByUserIds"), BeanUtil.toMap(query));
	}
}
